package com.handong.handserialprogram.module.voice;


/**
 * Copyright (C) 2017  Inc. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

//import com.bumptech.glide.Glide;
//import com.bumptech.glide.load.engine.DiskCacheStrategy;
//import com.bumptech.glide.request.target.SimpleTarget;
//import com.bumptech.glide.request.transition.Transition;
import com.handong.handserialprogram.R;
import com.handong.handserialprogram.constants.ColorContant;
import com.handong.handserialprogram.model.MessageBean;
import com.handong.handserialprogram.model.entities.FootRingNewEntity;
import com.handong.handserialprogram.model.status.FootRingStatus;
import com.handong.handserialprogram.module.createmission.searchring.SearchRingAdapter;
import com.handong.handserialprogram.thread.ImageDownloadThread;
import com.handong.handserialprogram.utils.BaseViewHolder;
import com.handong.handserialprogram.utils.ImageLoader;
import com.handong.handserialprogram.utils.SPUtil;
import com.handong.handserialprogram.utils.TimeUtils;
import com.ilike.voicerecorder.utils.CommonUtils;

import java.util.List;

import cc.shinichi.library.ImagePreview;

/**
 * desc:   消息适配器
 * author: wangshanhai
 * email: ilikeshatang@gmail.com
 * date: 2017/10/30 18:38
 */
public class EaseMessageAdapter extends BaseAdapter {

    private Context context;
    private List<MessageBean> list;

    private onItemClickLister onItemClickLister;


    public EaseMessageAdapter(Context mContext, List<MessageBean> list) {
        this.context = mContext;
        this.list = list;
        imageLoader=new ImageLoader(mContext.getApplicationContext());
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static int row=0;

    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder viewHolder;
        final MessageBean bean = list.get(position);
        //if (convertView == null) {
            viewHolder = new ViewHolder();
            if (bean.msg.equals(SPUtil.getStringValue(SPUtil.USER_NAME))){
                convertView = LayoutInflater.from(context).inflate(R.layout.ease_row_sent_voice, parent, false);
            }
            else{
                convertView = LayoutInflater.from(context).inflate(R.layout.ease_row_receive_voice, parent, false);
            }


            viewHolder.iv_voice = (ImageView) convertView.findViewById(R.id.iv_voice);
            viewHolder.tv_length = (TextView) convertView.findViewById(R.id.tv_length);
            viewHolder.timestamp = (TextView) convertView.findViewById(R.id.timestamp);
            viewHolder.bubble = (RelativeLayout) convertView.findViewById(R.id.bubble);
            viewHolder.bubble_text = (RelativeLayout) convertView.findViewById(R.id.bubble_text);
            viewHolder.tv_chatcontent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
            viewHolder.is_wait=(ProgressBar)convertView.findViewById(R.id.pbWait);
            viewHolder.tv_userName=(TextView)convertView.findViewById(R.id.tv_userNameContent);
            viewHolder.iv_content=(ImageView)convertView.findViewById(R.id.iv_chatcontent);
            viewHolder.bubble_image=(RelativeLayout)convertView.findViewById(R.id.bubble_image);

            //convertView.setTag(viewHolder);
        //} else {
        //    viewHolder = (ViewHolder) convertView.getTag();
        //}



        viewHolder.tv_userName.setText(bean.msg);

        viewHolder.timestamp.setText(TimeUtils.getTime(bean.time));
        //更改并显示录音条长度


        if (bean.datatype==1)
        {
            viewHolder.bubble_image.setVisibility(View.GONE);
            viewHolder.bubble_text.setVisibility(View.GONE);
            viewHolder.bubble.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams ps = (RelativeLayout.LayoutParams) viewHolder.bubble.getLayoutParams();
            ps.width = CommonUtils.getVoiceLineWight2(context, bean.second)+20;
            viewHolder.bubble.setLayoutParams(ps); //更改语音长条长度

            viewHolder.tv_length.setText(bean.second + "");
            viewHolder.iv_voice.setImageResource(R.drawable.ease_chatto_voice_playing);
            viewHolder.iv_voice.setVisibility(View.VISIBLE);
            viewHolder.bubble.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickLister != null) {
                        onItemClickLister.onItemClick(viewHolder.iv_voice, bean.path, position);
                    }
                }
            });
        }
        else if(bean.datatype==0){
            viewHolder.bubble_text.setVisibility(View.VISIBLE);
            viewHolder.bubble.setVisibility(View.GONE);
            viewHolder.bubble_image.setVisibility(View.GONE);
            viewHolder.tv_chatcontent.setText(bean.path);
            viewHolder.iv_voice.setVisibility(View.GONE);
        }
        else if(bean.datatype==2)
        {
            viewHolder.bubble_text.setVisibility(View.GONE);
            viewHolder.bubble.setVisibility(View.GONE);
            viewHolder.bubble_image.setVisibility(View.VISIBLE);
            //Glide.with(context).load(bean.path).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into(holder.ivImage);
            imageLoader.DisplayImage(bean.path, viewHolder.iv_content);
            viewHolder.iv_content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ImagePreview.getInstance().setContext(context).setImage(bean.path).start();
                }
            });
        }

        if (bean.iswaiting){
            viewHolder.is_wait.setVisibility(View.VISIBLE);
        }else{
            viewHolder.is_wait.setVisibility(View.GONE);
        }
        return convertView;
    }

    private class ViewHolder {
        ImageView iv_voice;
        TextView tv_length;
        TextView timestamp;
        RelativeLayout bubble;
        RelativeLayout bubble_text;
        TextView tv_chatcontent;
        ProgressBar is_wait;
        TextView tv_userName;
        RelativeLayout bubble_image;
        ImageView iv_content;
    }

    public interface onItemClickLister {
        void onItemClick(ImageView imageView, String path, int position);
    }

    public void setOnItemClickLister(EaseMessageAdapter.onItemClickLister onItemClickLister) {
        this.onItemClickLister = onItemClickLister;
    }
}

